package com.atguigu.jxc.controller;

/**
 * @author Sanmier
 * @create 2020-12-29 11:37
 */

import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.PurchaseListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @description 进货单保存
 */
@RestController
@RequestMapping("purchaseListGoods")
public class PurchaseListController {

    @Autowired
    private PurchaseListService purchaseListService;

    @Autowired
    private Gson gson;

    //http://localhost:8080/purchaseListGoods/save?purchaseNumber=JH1605768306735
    //进货单保存
    @PostMapping("save")
    public ServiceVO save(@RequestParam("purchaseNumber") String purchaseNumber,
                          PurchaseList purchaseList,
                          String purchaseListGoodsStr,
                          String supplierName,
                          HttpSession session){
        User currentUser = (User) session.getAttribute("currentUser");
        Integer userId = currentUser.getUserId();

        List<PurchaseListGoods> purchaseListGoodsList = gson.fromJson(purchaseListGoodsStr, new TypeToken<List<PurchaseListGoods>>() {}.getType());

        purchaseList.setUserId(userId);
        purchaseList.setPurchaseNumber(purchaseNumber);
        purchaseList.setSupplierName(supplierName);

        return purchaseListService.purchaseListGoodsListSave(purchaseList,purchaseListGoodsList);
    }


    //条件查询
    //http://localhost:8080/purchaseListGoods/list
    @PostMapping("list")
    public Map<String,Object> list(@Param("purchaseNumber") String purchaseNumber,
                                   @Param("supplierId") Integer supplierId,
                                   @Param("state") Integer state,
                                   @Param("sTime") String sTime,
                                   @Param("eTime") String eTime){

        return purchaseListService.getSearchList(purchaseNumber,supplierId,state,sTime,eTime);
    }


    //查询进货单商品信息
    //http://localhost:8080/purchaseListGoods/goodsList
    @PostMapping("goodsList")
    public Map<String,Object> goodsList(@Param("purchaseListId") Integer purchaseListId){

        return purchaseListService.getGoodsList(purchaseListId);
    }


    //http://localhost:8080/purchaseListGoods/delete
    //进货单删除
    @PostMapping("delete")
    public ServiceVO delete(@Param("purchaseListId") Integer purchaseListId){
        return purchaseListService.deletePurchaseListGoods(purchaseListId);
    }


    //支付结算（修改进货单付款状态）
    //http://localhost:8080/purchaseListGoods/updateState
    @PostMapping("updateState")
    public ServiceVO updateState(@Param("purchaseListId") Integer purchaseListId){
        return purchaseListService.updatePurchaseListGoods(purchaseListId);
    }

}
